#!/usr/bin/python
from Config import *
from User import *
import linecache
from operator import itemgetter, attrgetter

class HtmlExport(Config):
    def __init__(self,D = User()):
        self.AD = D 
        self.File = open(self.HTML,"w")
    def header(self):
        H = "<html>\n<head>\n<title>Stats for "+self.Nick+" </title>\n</head>\n"
        return H
    def body(self):
        H = "<body>\n<div width=100>"
        for i in self.HTMLLabel:
            H+="<strong>"+i+": </strong><br>"
        H+="</div><div style=position:absolute;left:100px;top:10>"
        H+=self.Nick+"<br>"
        H+=str(self.AD.You.Kills)+"<br>"
        H+=str(self.AD.You.Death)+"<br>"
        o = "{liczba:.2f}%".format(liczba = float(self.AD.You.Kills)/float(self.AD.You.Death))
        H+=o+"<br>"
        o = "{liczba:.2f}%".format(liczba =  float(self.AD.You.YourHit)*100/float(self.AD.You.EmnyHit))
        H+=str(self.AD.You.YourHit)+" / "+str(self.AD.You.EmnyHit)+" "+o+"<br>"
        H+="</div>"
        H+="<h3>Hits Stats</h3><table><tr><th>Zone</th><th>You</th><td>*</td><th>Enemies</th></tr>"
        for i in range(len(self.Zones)):
            r = "{liczba:.2f}%".format(liczba =  float(self.AD.You.YourHitZone[i])*100/float(self.AD.You.YourHit))
            s = "{liczba:.2f}%".format(liczba =  float(self.AD.You.EmnyHitZone[i])*100/float(self.AD.You.EmnyHit))
            H+= "<tr><th>"+self.Zones[i]+"</th>"+"<td>"+str(self.AD.You.YourHitZone[i])+"</td><td> ("+str(r)+") </td><td>"+str(self.AD.You.EmnyHitZone[i])+"</td><td>("+str(s)+")</td></tr>"
        H+="</table><h3>CTF Stats</h3><table ><tr ><th>Action</th></tr>"
        for i in range(len(self.Actions)):
            H+= "<tr><th>"+self.Actions[i]+"</th>"+"<td>"+str(self.AD.You.FlagAction[i])+"</td></tr>"
        H+="</table><h3>Teams Stats</h3><table ><tr ><th>Team</th><th>*</th></tr>"
        H+="<tr><td>Red</td><td>"+str(self.AD.You.Teams[0])+"</td></tr><td>Blue</td><td>"+str(self.AD.You.Teams[1])+"</td></tr>"
        H+="<table>"
        H+="</table><h3>Death/Kills Stats</h3><table ><tr ><th>Player</th><th>Deaths</th><th>Kills</th></tr>"
        self.AD.You.PlayersDK.sort(key=lambda x: x[1])
        for i in xrange(len(self.AD.You.PlayersDK)-1,-1,-1):
            F = 0 
            for a in self.Hightlight:
                if a==self.AD.You.PlayersDK[i][0]:
                        H+= "<tr><td><strong>"+self.AD.You.PlayersDK[i][0]+"</strong></td>"+"<td>"+str(self.AD.You.PlayersDK[i][1])+"</td><td>--</td></tr>"
                        F = 1
                        break
            if F==0:
                if self.MinScoreD<=self.AD.You.PlayersDK[i][1] and self.MinScoreK<=self.AD.You.PlayersDK[i][1]:
                    H+= "<tr><td>"+self.AD.You.PlayersDK[i][0]+"</td>"+"<td>"+str(self.AD.You.PlayersDK[i][1])+"</td><td>"+str(self.AD.You.PlayersDK[i][2])+"</td></tr>"
        H+="</table></body>"
        return H
    def end(self):
        H = "</html>"
        return H
    def genFile(self):
        F = self.header()
        F+= self.body()
        F+=self.end()
        self.File.write(F)
        self.File.close()